<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

require_once './resources/org/code/Code.class.php';
class LoginController extends CommonController
{
    // 存放验证码对象
    protected $code = null;
    public function __construct(){
       if(!$this->code instanceof \Code){
            $this->code = new \Code;
       }
    }
    public function login(){
      if($input = Input::all()){
          // 验证验证码
          if($_SESSION['code']!= strtoupper($input['code'])){
              return back()->with('login_info','验证码错误!');
          }
          // 验证用户名及密码
          $user = DB::table('user')->first();

          if($user->user_name == $input['user_name'] && decrypt($user->user_pass) == $input['user_pass']){
              session(['user'=>$user]);
                return redirect('admin');
          }else{
                return back()->with('login_info','用户名或密码错误!');
          }

      }else{
       return view('admin.login');
      }
    }

    // 输出验证码
    public function code(){
        $this->code->make();
    }

    // 退出
    public function quit(){
        session(['user'=>null]);
        return redirect('admin/login');
    }
}
